<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/8 0008
 * Time: 上午 10:52
 */

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator;
use Session;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Models\CustomModel;

class CustomController extends Controller
{
    public function index()
    {
        //
        $data['data'] = CustomModel::paginate(10);
        $data['type']=3 ;
        return view('custom.index',compact('data'));
    }
    public function edit(Request $request,$id)
    {
        if ($request->isMethod('get')){
            $data = CustomModel::where(['id' => $id])->first()->toArray();
            return view('custom.edit', compact('data'));
        }else{
            $data = $request->except(['_token','uploadfile']);
            $validator = Validator::make($data, [
                'content'     => 'required|string',
                'prompt'     => 'required|string',
            ]);
            if ($validator->fails()) {
                return redirect('/admin/custom/edit')
                    ->withErrors($validator)
                    ->withInput($data);
            }
            $res= CustomModel::where(['id'=>$id])->update($data);

            $data['data'] = CustomModel::paginate(10);
            $data['type']=3 ;
            return view('custom.index',compact('data'));
        }


    }

    public function postedit(Request $request)
    {
        $data = $request->except(['_token','uploadfile']);
        $validator = Validator::make($data, [
            'content'     => 'required|string',
            'prompt'     => 'required|string',
        ]);
        if ($validator->fails()) {
            return redirect('/admin/custom/edit/'.$data['id'])
                ->withErrors($validator)
                ->withInput($data);
        }
        CustomModel::where(['id'=>$data['id']])->update($data);
        return redirect('/admin/custom/index');

    }








}